package controller;

import model.Card;
import view.UI;

import javax.swing.*;
import java.awt.event.*;

public class MyTimer {
    private Timer timer;
    private ActionListener actionListener;
    private Controller controller;
    private JaoFenTimer jaoFenTimer;


    public MyTimer(int delay, Controller controller) {

        this.controller = controller;
        actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                UI.enterGameJFrame.enterGameJPanel.eastTextField.setVisible(false);
                UI.enterGameJFrame.enterGameJPanel.westTextField.setVisible(false);
                UI.enterGameJFrame.enterGameJPanel.jTextField.setVisible(false);
                if (controller.getAllList().size() == 3) {
                    for (int i = 0; i < 3; i++) {
                        controller.underPlayer.list.add(controller.getAllList().remove(0));
                    }
                } else if (controller.getAllList().size() == 0) {

                    stop();
                    UI.enterGameJFrame.enterGameJPanel.jaoFen1Button.setVisible(true);
                    UI.enterGameJFrame.enterGameJPanel.jaoFen2Button.setVisible(true);
                    UI.enterGameJFrame.enterGameJPanel.jaoFen3Button.setVisible(true);
                    UI.enterGameJFrame.enterGameJPanel.noJaoButton.setVisible(true);
                    UI.enterGameJFrame.enterGameJPanel.westDeZhuField.setVisible(false);
                    UI.enterGameJFrame.enterGameJPanel.soutDeZhuField.setVisible(false);
                    UI.enterGameJFrame.enterGameJPanel.eastDeZhuField.setVisible(false);
                    jaoFenTimer = new JaoFenTimer(30, controller);
                    UI.enterGameJFrame.enterGameJPanel.openCards.setVisible(true);
                    upCard();
                } else {
                    controller.nowPlayer.list.add(controller.getAllList().remove(0));
                    controller.nowPlayer = controller.nowPlayer.nextPlayer;
                }
            }
        };

        timer = new Timer(delay, actionListener);
        start();
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    public void upCard() {

            for (int i = 0; i < controller.getPersonPlayer().list.size(); i++) {
                Card card = controller.getPersonPlayer().list.get(i);

                UI.enterGameJFrame.enterGameJPanel.southPanel.setLayout(null);
                card.setBounds(300 + i * 40, 150, 50, 80);

                UI.enterGameJFrame.enterGameJPanel.southPanel.add(card);
                UI.enterGameJFrame.enterGameJPanel.southPanel.setComponentZOrder(card, 0);

            }


        System.out.println(UI.enterGameJFrame.enterGameJPanel.southPanel.getLayout());

    }
}
